<template>
  <div class="store-list">
    <el-row :gutter="10" justify="end">
      <el-col :span="3.5"><el-input placeholder="输入仓库名字查询" v-model.trim="store_name" /></el-col>
      <el-col :span="3.5"><el-input placeholder="输入供应商名字查询" v-model.trim="supplier_name" /></el-col>
      <el-col :span="3.5">
        <el-form-item>
          <el-select 
          v-model="state"
            placeholder="请选择"
            style="width: 240px"
            filterable>
              
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="1"><el-button type="primary" @click="handleSearch">查询</el-button></el-col>
      <el-col :span="1"><el-button @click="handleEdit">新增</el-button></el-col>
    </el-row>
  </div>
  <el-divider />

  <el-table
  :data="storeList"
  style="width: 1680px"
  border
  :header-cell-style="{background:'rgb(250,250,250)'}"
  >
    <el-table-column prop="store_name" label="仓库名" width="300" />
    <el-table-column prop="state" label="状态" width="120" />
    <el-table-column prop="purchase_username" label="收货人" width="200" />
    <el-table-column prop="supplier_name" label="供应商" width="100" />
    <el-table-column prop="purchase_cause" label="采购原因" width="290" />
    <el-table-column prop="purchase_time" label="采购时间" width="290" />
    <el-table-column prop="address" label="收获地址" width="170" />
    <el-table-column label="操作" width="200" ><a href="./detail">详情</a></el-table-column>
    <template #empty>
      <el-empty description="暂无数据" />
    </template>
  </el-table>

  <el-pagination
  background
  layout="prev, pager, next"
  :total="total"
  :page-size="sizes"
  @current-change="handlePage"
  />
</template>

<script>
import { addListApi,getListApi,approveListApi } from '@/apis/Purchase/instore'

export default {
  data() {
    return {
      store_name: '',
      supplier_name: '',
      state: '',
      pages: 1,
      sizes: 10,
      total: 0,
      storeList: [],
      options: [
        {
          value: '',
          label: '全部',
        },
        {
          value: 'Add',
          label: '新建',
        },
        {
          value: 'Archive',
          label: '完成',
        },
        {
          value: 'Finance',
          label: '归档',
        },
      ]
    }
  },
  created() {
    this.handleGet()
  },
  methods: {
    handleEdit() {
      this.$router.push('./edit')
    },

    async handleGet(supplier_name, store_name, state, size = this.sizes, page = this.pages) {
      try {
        const res = await getListApi({supplier_name, store_name, state, size, page})
        this.storeList = res.rows
        this.total = res.count

      } catch (error) {
        console.log(error)
      }
    },
    handleSearch() {
      this.storeList.forEach(item => {
        if(item.store_name.includes(this.store_name)) {
          this.handleGet(this.supplier_name, this.store_name, this.state)
        }
        else if(item.supplier_name.includes(this.supplier_name)) {
          this.handleGet(this.supplier_name, this.store_name, this.state)
        }
        else if(item.state === this.state) {
          this.handleGet(this.supplier_name, this.store_name, this.state)
        }
      })
    },
    handlePage(e) {
      this.pages = e;
      this.handleGet()
    }
  }
}

</script>

<style lang="less">
.el-row {
  margin-bottom: 20px;
}
.el-row:last-child {
  margin-bottom: 0;
}
.el-col {
  border-radius: 4px;
}
.el-input {
  width: 220px;
}

.el-pagination {
  width: 300px;
  position: absolute;
  margin-top: 30px;
  left: 45%;
}

a {
  text-decoration: none;
}

.el-table--border,
.el-table--group {
  border: 1px solid rgb(236, 236, 236);
}
</style>
